<?php 
//include the information needed for the connection to SQLite filename and pseudo-database server. 
// we store here username, database and password 
require_once($_SERVER['DOCUMENT_ROOT'] . "/config.php");
include(ABS_PATH . "misc/functions.php");

// to the url parameter are added 4 parameters as described in colModel
// we should get these parameters to construct the needed query
// Since we specify in the options of the grid that we will use a GET method 
// we should use the appropriate command to obtain the parameters. 
// In our case this is $_GET. If we specify that we want to use post 
// we should use $_POST. Maybe the better way is to use $_REQUEST, which
// contain both the GET and POST variables. For more information refer to php documentation.
// Get the requested page. By default grid sets this to 1. 
$page = $_REQUEST['page']; 

// get how many rows we want to have into the grid - rowNum parameter in the grid 
$limit = $_REQUEST['rows']; 

// get index row - i.e. user click to sort. At first time sortname parameter -
// after that the index from colModel 
$sidx = $_REQUEST['sidx']; 

// sorting order - at first time sortorder 
$sord = $_REQUEST['sord']; 

// if we not pass at first time index use the first column for the index or what you want
if(!$sidx) $sidx =1; 

// connect to the database 
try {
	$db = new PDO(PDO_PREFIX . SQLITE_DB_FILENAME);
}

catch(PDOException $e)
{
	die("Connection Error to database " . PDO_PREFIX . SQLITE_DB_FILENAME . ": " . $e->getMessage());
}

/* if (!($db = new SQLite3(SQLITE_DB_FILENAME)))
	die("Connection Error to database " . SQLITE_DB_FILENAME);
*/

_log("Opened " . PDO_PREFIX . SQLITE_DB_FILENAME);

$result = NULL; $count = 0; // for scope issues

try {
	// calculate the number of rows for the query. We need this for paging the result 
	$columns = $db->query("SELECT COUNT(*) AS count FROM Inventory");
	$count = $columns->fetchColumn(); 
	//if (!$count)
	//	die("Query error on database " . SQLITE_DB_FILENAME . ":" . $db->lastError());
	//$count = $result->fetchSingle(); 
	//$count = $row['count']; 
	
	_log(sprintf("Counted %d row(s)", $count));
	
	// calculate the total pages for the query 
	if( $count > 0 ) { 
				  $total_pages = ceil($count/$limit); 
	} else { 
				  $total_pages = 0; 
	} 
	
	// if for some reasons the requested page is greater than the total 
	// set the requested page to total page 
	if ($page > $total_pages) $page=$total_pages;
	
	// calculate the starting position of the rows 
	$start = $limit*$page - $limit;
	
	// if for some reasons start position is negative set it to 0 
	// typical case is that the user type 0 for the requested page 
	if($start <0) $start = 0; 
	
	// the actual query for the grid data 
	$SQL = "SELECT * FROM Inventory ORDER BY $sidx $sord LIMIT $start, $limit"; 
	$result = $db->query($SQL);
}

catch(PDOException $e)
{
	die("Couldn't execute query $SQL: " . $e->getMessage());
}

// we should set the appropriate header information
if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
			  header("Content-type: application/xhtml+xml;charset=utf-8"); 
} else {
		  header("Content-type: text/xml;charset=utf-8");
}
echo "<?xml version='1.0' encoding='utf-8'?>";
echo "<rows>";
echo "<page>".$page."</page>";
echo "<total>".$total_pages."</total>";
echo "<records>".$count."</records>";

try {
	// be sure to put text data in CDATA
	while($row = $result->fetch(PDO::FETCH_ASSOC)) {
		echo "<row id='". $row['UUID'] . "'>";			
		echo "<cell>" . $row['UUID']."</cell>";
		echo "<cell>" . utf8_encode($row['Name']) . "</cell>";
		echo "<cell>" . utf8_encode($row['Type']) . "</cell>";
		echo "<cell>" . $row['Permissions'] . "</cell>";
		echo "<cell>" . $row['LastUpdate'] . "</cell>";
		echo "</row>";
	}
}

catch(PDOException $e)
{
	_log(__FILE__ . ":" . __LINE__ . " No rows for query $SQL");
}
echo "</rows>"; 
?>